package com.bjpowernode.javase.collection;

import java.util.TreeSet;

/*
先按照年龄升序如果年两一样的在按照姓名升序。
 */
public class TreeSetTest05 {
    public static void main(String[] args) {
        TreeSet<Vip> vips = new TreeSet<>();
        vips.add(new Vip("zhangan",25));
        vips.add(new Vip("lisi",23));
        vips.add(new Vip("Misi",23));
        vips.add(new Vip("king",18));
        vips.add(new Vip("soft",17));

        for(Vip vip:vips){
            System.out.println(vip);
        }

    }
}

class Vip implements Comparable<Vip> {
    String name;
    int age;
    public Vip(String name,int age){
        this.age = age;
        this.name = name;
    }

    /*@Override
    public String toString() {
        return "Vip{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }*/

    public String toString(){
        return "Vip{" +
                "name='" + name + '\'' +
                ",age=" + age +
                '}';
    }

    /*@Override
    public int compareTo(Vip o) {
        return 0;
    }*/

    /*
    compareTo方法的返回值很重要：
        返回值为0会覆盖。
        返回值大于零，会继续在右子数上找。
        返回值小于零，会继续在左子树上找。
     */
    public int compareTo(Vip v){
        if(this.age == v.age){
            //年龄相同时按照名字排序
            return this.name.compareTo(v.name);
        }else{
            //年龄不一样
            return this.age - v.age;
        }

    }
}
